package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-12-21 18:06
 **/
public class LeetCode461 {

    //Brian Kernighan算法
    public int hammingDistance(int x, int y) {
        int s = x ^ y, ret = 0;
        while (s != 0) {
            s &= s - 1;
            ret++;
        }
        return ret;
    }


    //api调度
    /*public int hammingDistance(int x, int y) {
        return Integer.bitCount(x ^ y);
    }*/

    //移位实现位计数
    /*public int hammingDistance(int x, int y) {
        int k = x ^ y;
        int res = 0;

        while (k > 0) {
            //判断当前0位是否为1,为1,汉明距离+1
            res += k & 1;
            k >>= 1;

        }

        return res;
    }*/
}
